package com.jhgsys.internal.formDesign.service.impl;

import com.jhgsys.internal.formDesign.entity.FormPublishObject;
import com.jhgsys.internal.formDesign.mapper.FormPublishObjectMapper;
import com.jhgsys.internal.formDesign.service.IFormPublishObjectService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 表单发布对象 Service实现
 *
 * @author zhangwei
 * @date 2024-09-03 10:58:07
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class FormPublishObjectServiceImpl extends ServiceImpl<FormPublishObjectMapper, FormPublishObject> implements IFormPublishObjectService {

    /**
     * 查询（分页）
     * @author zhangwei
     * @date 2024-09-03 10:58:07
     * @param request QueryRequest
     * @param formPublishObject formPublishObject
     * @return IPage<FormPublishObject>
    */
    @Override
    public IPage<FormPublishObject> findFormPublishObjectPage(QueryRequest request, FormPublishObject formPublishObject) {
        LambdaQueryWrapper<FormPublishObject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FormPublishObject::getDeletedFlg, 0);
        // TODO 设置查询条件

        Page<FormPublishObject> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author zhangwei
     * @date 2024-09-03 10:58:07
     * @param formPublishObject formPublishObject
     * @return List<FormPublishObject>
     */
    @Override
    public List<FormPublishObject> findFormPublishObjectList(FormPublishObject formPublishObject) {
	    LambdaQueryWrapper<FormPublishObject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FormPublishObject::getDeletedFlg, 0);
        if(StringUtils.isNotBlank(formPublishObject.getTenantId())){
            queryWrapper.eq(FormPublishObject::getTenantId, formPublishObject.getTenantId());
        }
        if(StringUtils.isNotBlank(formPublishObject.getDesignId())){
            queryWrapper.eq(FormPublishObject::getDesignId, formPublishObject.getDesignId());
        }
        if(StringUtils.isNotBlank(formPublishObject.getObjectId())){
            queryWrapper.eq(FormPublishObject::getObjectId, formPublishObject.getObjectId());
        }
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author zhangwei
    * @date 2024-09-03 10:58:07
    * @param id
    * @return FormPublishObject
    */
    @Override
    public FormPublishObject findFormPublishObjectById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author zhangwei
     * @date 2024-09-03 10:58:07
     * @param formPublishObject formPublishObject
     */
    @Override
    @Transactional
    public void createFormPublishObject(FormPublishObject formPublishObject) {
        formPublishObject.setCreatedTime(new Date());
        this.save(formPublishObject);
    }

    /**
     * 修改
     * @author zhangwei
     * @date 2024-09-03 10:58:07
     * @param formPublishObject formPublishObject
     */
    @Override
    @Transactional
    public void updateFormPublishObject(FormPublishObject formPublishObject) {
        formPublishObject.setUpdatedTime(new Date());
        this.saveOrUpdate(formPublishObject);
    }

    /**
     * 逻辑删除
     * @author zhangwei
     * @date 2024-09-03 10:58:07
     * @param formPublishObject formPublishObject
     * @param formPublishObjectIds formPublishObjectIds
     */
    @Override
    @Transactional
    public void deleteFormPublishObject(FormPublishObject formPublishObject,String formPublishObjectIds) {
        List<String> list = Arrays.asList(formPublishObjectIds.split(StringPool.COMMA));
        this.baseMapper.update(formPublishObject,new LambdaQueryWrapper<FormPublishObject>().in(FormPublishObject::getPublishObjectId,list));
	}

    @Override
    public List<FormPublishObject> selectByDesignId(String id) {
        LambdaQueryWrapper<FormPublishObject> wapper = new LambdaQueryWrapper<>();
        wapper.eq(FormPublishObject::getDesignId,id);
        wapper.eq(FormPublishObject::getDeletedFlg,0);
        return this.list(wapper);
    }

    /**
    * 物理删除
    * @author zhangwei
    * @date 2024-09-03 10:58:07
    * @param formPublishObjectIds formPublishObjectIds
    */
    @Override
    @Transactional
    public void physicallyDeleteFormPublishObject(String formPublishObjectIds) {
        List<String> list = Arrays.asList(formPublishObjectIds.split(StringPool.COMMA));
        LambdaQueryWrapper<FormPublishObject> wapper = new LambdaQueryWrapper<>();
        wapper.in(FormPublishObject::getPublishObjectId,list);
        this.remove(wapper);
    }
}
